package Controlador;

import Modelo.SA;
import Problema.todo;
import Problema.camino;
import Problema.deposito;
import Problema.tarea;
import Problema.tracto;
import Vista.VPrincipal;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;
import Modelo.Solucion;
import Problema.tramo;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;

public class CPrincipal {
    VPrincipal vista;
    File destinos, tareas, tracto, depositos, cargas, destDep, log;
    todo todo;
    SA sa;

    public CPrincipal() {

        //DESCOMENTÉ LA VISTA PRINCIPAL PARA PROBAR LA GENERACION DEL VECINO
        //Solucion a = new Solucion();
        //a.genVecino();
        /*******************************************************************/
        
        this.vista = new VPrincipal(this);
        this.vista.setVisible(true);
        this.todo = new todo(this);
    }

    public void setArchivo(int i, File f) {
        switch(i) {
            case 1:
                this.destinos = f;
                break;
            case 2:
                this.tareas = f;
                break;
            case 3:
                this.tracto = f;
                break;
            case 4:
                this.depositos = f;
                break;
            case 5:
                this.cargas = f;
                break;
            case 6:
                this.destDep = f;
                break;
        }
    }

    public void procesar() {
        this.pDestinos();
        this.pTareas();
        this.todo.procesarTareas();
        this.pTractos();
        this.pDepositos();
        this.pCargas();
        this.pTramos();
    }

    public void pDestinos() {
        FileReader fR;
        BufferedReader bR;
        try {
            fR = new FileReader(this.destinos);
            bR = new BufferedReader(fR);
            String texto;
            String[] txt, txt2;
            int j, i;
            camino aux;
            Vector dest = new Vector();
            Vector linea;
            if(this.destinos.exists()) {
                i = 0;
                while((texto = bR.readLine())!=null) {
                    linea = new Vector<camino>();
                    txt = texto.split(" ");
                    for(j=0; j<txt.length; j++) {
                        if(txt[j].compareTo("-")!=0) {
                            aux = new camino();
                            txt2 = txt[j].split(",");
                            aux.setDeposito(txt2[0]);
                            aux.setDistancia(Double.parseDouble(txt2[1]));
                            aux.setTiempo(Double.parseDouble(txt2[2]));
                            aux.setcIni(i+1);
                            aux.setcFin(j+1);
                            linea.add(aux);
                        }
                    }
                    dest.add(linea);
                    i++;
                }
            }
            fR.close();
            bR.close();
            this.todo.setDestinos(dest);
        } catch(IOException e) {
        }
    }

    public void pTareas() {
        FileReader fR;
        BufferedReader bR;
        try {
            fR = new FileReader(this.tareas);
            bR = new BufferedReader(fR);
            String texto;
            String[] txt;
            tarea aux;
            Vector<tarea>r = new Vector<tarea>();
            if(this.tareas.exists()) {
                while((texto = bR.readLine())!=null) {
                    aux = new tarea();
                    txt = texto.split(" ");
                    aux.setcIni(Integer.parseInt(txt[0]));
                    aux.setcFin(Integer.parseInt(txt[1]));
                    aux.settCarga(Integer.parseInt(txt[2]));
                    aux.setCarga(Integer.parseInt(txt[3]));
                    r.add(aux);
                }
            }
            fR.close();
            bR.close();
            this.todo.setTareas(r);
        } catch(IOException e) {
        }
    }

    public void pTractos() {
        FileReader fR;
        BufferedReader bR;
        try {
            fR = new FileReader(this.tracto);
            bR = new BufferedReader(fR);
            String texto;
            String[] txt;
            Vector<tracto>r = new Vector<tracto>();
            tracto aux;
            if(this.tracto.exists()) {
                while((texto = bR.readLine())!=null) {
                    aux = new tracto();
                    txt = texto.split(" ");
                    if(txt[0].compareTo("-")!=0)
                        aux.setSemiActual(Integer.parseInt(txt[0]));
                    else
                        aux.setSemiActual(-1);
                    aux.setPosActual(txt[2]);
                    if(txt[1].compareTo("-")!=0) {
                        int tId = Integer.parseInt(txt[1].substring(0, txt[1].length()-1));
                        aux.settActual(tId);
                        if(txt[1].substring(txt[1].length()-1).compareTo("+")==0) {
                            this.todo.getTarea(tId).setEstado(0);
                        } else {
                            this.todo.getTarea(tId).setEstado(1);
                        }
                    } else
                        aux.settActual(-1);
                    r.add(aux);
                }
            }
            fR.close();
            bR.close();
            this.todo.setTractos(r);
        } catch(IOException e) {
        }
    }

    public void pDepositos() {
        FileReader fR;
        BufferedReader bR;
        try {
            fR = new FileReader(this.depositos);
            bR = new BufferedReader(fR);
            String texto;
            String[] txt;
            Vector<deposito>r = new Vector<deposito>();
            deposito aux;
            if(this.depositos.exists()) {
                while((texto = bR.readLine())!=null) {
                    aux = new deposito();
                    txt = texto.split(" ");
                    aux.setNombre(txt[0]);
                    aux.setTr(Integer.parseInt(txt[1]));
                    aux.setCb(Integer.parseInt(txt[2]));
                    aux.setFu(Integer.parseInt(txt[3]));
                    aux.setEs(Integer.parseInt(txt[4]));
                    aux.setRa(Integer.parseInt(txt[5]));
                    aux.setCo(Integer.parseInt(txt[6]));
                    r.add(aux);
                }
            }
            fR.close();
            bR.close();
            this.todo.setDepositos(r);
        } catch(IOException e) {
        }
    }

    public void pCargas() {
        FileReader fR;
        BufferedReader bR;
        try {
            fR = new FileReader(this.cargas);
            bR = new BufferedReader(fR);
            String texto;
            String[] txt;
            Vector<int[]>r = new Vector<int[]>();
            int[] aux;
            if(this.cargas.exists()) {
                while((texto = bR.readLine())!=null) {
                    aux = new int[2];
                    txt = texto.split(" ");
                    aux[0] = Integer.parseInt(txt[0]);
                    aux[1] = Integer.parseInt(txt[1]);
                    r.add(aux);
                }
            }
            fR.close();
            bR.close();
            this.todo.setCargaSR(r);
        } catch(IOException e) {
        }
    }

    public void pTramos() {
        FileReader fR;
        BufferedReader bR;
        try {
            fR = new FileReader(this.destDep);
            bR = new BufferedReader(fR);
            String texto;
            String[] txt, txt2;
            int j, i;
            tramo aux;
            Vector dest = new Vector();
            Vector linea;
            if(this.destDep.exists()) {
                i = 0;
                while((texto = bR.readLine())!=null) {
                    linea = new Vector<tramo>();
                    txt = texto.split(" ");
                    for(j=0; j<txt.length; j++) {
                        txt2 = txt[j].split(",");
                        if(txt2.length==3) {
                            aux = new tramo();
                            aux.setCiudad(i+1);
                            aux.setDeposito(txt2[0]);
                            aux.setDistancia(Double.parseDouble(txt2[1]));
                            aux.setTiempo(Double.parseDouble(txt2[2]));
                            linea.add(aux);
                        }
                    }
                    dest.add(linea);
                    i++;
                }
            }
            fR.close();
            bR.close();
            this.todo.setTramos(dest);
        } catch(IOException e) {
        }
    }

    public void iniciar(int ti, int tf, double en, int md, int mc, int ca) {
        this.log = new File("log_"+ti+"_"+tf+"_"+en+".txt");
        this.sa = new SA(this);
        this.sa.setPorcEnfriamiento(en);
        this.sa.setTempInicial(ti);
        this.sa.setTempFinal(tf);
        this.sa.run();
    }

    public SA getSa() {
        return sa;
    }

    public void setSa(SA sa) {
        this.sa = sa;
    }

    public todo getTodo() {
        return todo;
    }

    public void setTodo(todo todo) {
        this.todo = todo;
    }

    public void grabar(int t, Vector costo) {
        FileWriter fW;
        BufferedWriter bW;
        PrintWriter salida;
        int l = costo.size(), i;
        try {
            fW = new FileWriter(this.log, true);
            bW = new BufferedWriter(fW);
            salida = new PrintWriter(bW);
            for(i = 0; i<l; i++) {
                salida.println(t+"\t"+costo.get(i));
            }
            salida.close();
            bW.close();
            fW.close();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }

}
